package com.saltedfish.tool.ui.achievement

import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.droidlover.xdroidmvp.base.SimpleListener
import cn.droidlover.xdroidmvp.log.XLog
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.saltedfish.tool.R
import com.saltedfish.tool.dao.SubFlagModelDao
import com.saltedfish.tool.model.FlagTable
import com.saltedfish.tool.ui.flag.adapter.SubFlagAdapter

class AchievementAdapter(lists: List<FlagTable>) :
    BaseQuickAdapter<FlagTable, BaseViewHolder>(
        R.layout.item_flag,
        lists
    ) {

    fun setSubFlagModelDao(subFlagModelDaos: SubFlagModelDao) {
        this.subFlagModelDao = subFlagModelDaos
    }

    private var subFlagModelDao: SubFlagModelDao ? = null

    private var listener: SimpleListener<*>? = null

    private var linearLayoutManager: LinearLayoutManager? = null
    private var adapter: SubFlagAdapter? = null

    fun setListener(listener: SimpleListener<*>?) {
        this.listener = listener
    }

    override fun convert(helper: BaseViewHolder?, item: FlagTable) {

        var iv_flag_item = helper?.getView<RelativeLayout>(R.id.iv_flag_item)
        var flagContent = helper?.getView<TextView>(R.id.flagContent)
        var flagTime = helper?.getView<TextView>(R.id.flagTime)

        var ivSonFlag = helper?.getView<ImageView>(R.id.ivSonFlag)
        var flagDetail = helper?.getView<RelativeLayout>(R.id.flagDetail)
        var mRecyclerView = helper?.getView<RecyclerView>(R.id.mRecyclerView)
        ivSonFlag!!.visibility = View.GONE
        flagContent!!.text = item.title
        if(item.isComplete){
            var completeDay = item.completeDay!!.replace("-","/")
            flagTime!!.text = completeDay + "完成"
        }
//        XLog.e("-----展示的成就页面---$item")

        if(subFlagModelDao !=null){
          var  subFlagLists = subFlagModelDao!!.getAllByFlagIdAndProgress(item.flagId,1.0f,"提前完成")
//            XLog.e("-----展示的成就页面---$subFlagLists")
            if(subFlagLists.size ==0){
                iv_flag_item!!.visibility = View.GONE
            }else{
                iv_flag_item!!.visibility = View.VISIBLE
               // var position = helper!!.adapterPosition

                flagDetail!!.setOnClickListener {
                    if (listener !=null){
                        listener!!.onClick(item.flagId,"detail")
                    }
                }
                if(!item.isComplete){
                    linearLayoutManager = LinearLayoutManager(mContext)
                    mRecyclerView?.layoutManager = linearLayoutManager
                    adapter = SubFlagAdapter(subFlagLists,item.color,2)
                    mRecyclerView?.adapter = adapter
                }else{

                }

            }
        }



    }


}